#include "user_pwd_dialog.h"
#include "com/usermgr/usermgr.h"
#include "com/sysmgr/sysmgr.h"
#include "com/virt_kb/virt_kb.h"

// 前向声明
static void btn_ok_event_cb(lv_event_t* e);
static void btn_cancel_event_cb(lv_event_t* e);
static void update_user_list(lv_obj_t* dd);
static void ta_event_cb(lv_event_t* e);

extern virt_kb_t* g_virt_kb;

// 创建修改密码对话框
user_pwd_dialog_t* user_pwd_dialog_create(lv_obj_t* parent, void (*close_cb)(void)) {
    // 分配内存
    user_pwd_dialog_t* dialog = (user_pwd_dialog_t*)lv_mem_alloc(sizeof(user_pwd_dialog_t));
    if (dialog == NULL) {
        return NULL;
    }
    lv_memset(dialog, 0, sizeof(user_pwd_dialog_t));

    dialog->close_cb = close_cb;

    // 创建对话框
    dialog->obj = lv_obj_create(parent);
    lv_obj_set_size(dialog->obj, 340, 260);
    lv_obj_center(dialog->obj);
    lv_obj_set_style_pad_all(dialog->obj, 10, 0);

    // 创建标题
    lv_obj_t* title = lv_label_create(dialog->obj);
    lv_label_set_text(title, "Change Password");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 0);

    // 创建用户选择区域的容器
    lv_obj_t* user_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(user_cont, 300, 40);
    lv_obj_align(user_cont, LV_ALIGN_TOP_LEFT, 0, 40);
    lv_obj_set_style_pad_all(user_cont, 0, 0);
    lv_obj_set_style_border_width(user_cont, 0, 0);
    lv_obj_set_flex_flow(user_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(user_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建用户选择标签和下拉框
    lv_obj_t* user_label = lv_label_create(user_cont);
    lv_label_set_text(user_label, "Select User:");
    lv_obj_set_style_min_width(user_label, 105, 0);

    dialog->user_dd = lv_dropdown_create(user_cont);
    lv_obj_set_size(dialog->user_dd, 180, 40);
    update_user_list(dialog->user_dd);

    // 创建旧密码输入区域的容器
    lv_obj_t* old_pwd_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(old_pwd_cont, 300, 40);
    lv_obj_align(old_pwd_cont, LV_ALIGN_TOP_LEFT, 0, 90);
    lv_obj_set_style_pad_all(old_pwd_cont, 0, 0);
    lv_obj_set_style_border_width(old_pwd_cont, 0, 0);
    lv_obj_set_flex_flow(old_pwd_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(old_pwd_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建旧密码标签和输入框
    lv_obj_t* old_pwd_label = lv_label_create(old_pwd_cont);
    lv_label_set_text(old_pwd_label, "Old Password:");
    lv_obj_set_style_min_width(old_pwd_label, 105, 0);

    dialog->old_pwd_ta = lv_textarea_create(old_pwd_cont);
    lv_textarea_set_password_mode(dialog->old_pwd_ta, true);
    lv_obj_set_size(dialog->old_pwd_ta, 180, 40);
    lv_obj_add_event_cb(dialog->old_pwd_ta, ta_event_cb, LV_EVENT_ALL, NULL);

    // 创建新密码输入区域的容器
    lv_obj_t* new_pwd_cont = lv_obj_create(dialog->obj);
    lv_obj_set_size(new_pwd_cont, 300, 40);
    lv_obj_align(new_pwd_cont, LV_ALIGN_TOP_LEFT, 0, 140);
    lv_obj_set_style_pad_all(new_pwd_cont, 0, 0);
    lv_obj_set_style_border_width(new_pwd_cont, 0, 0);
    lv_obj_set_flex_flow(new_pwd_cont, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(new_pwd_cont, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    // 创建新密码标签和输入框
    lv_obj_t* new_pwd_label = lv_label_create(new_pwd_cont);
    lv_label_set_text(new_pwd_label, "New Password:");
    lv_obj_set_style_min_width(new_pwd_label, 105, 0);

    dialog->new_pwd_ta = lv_textarea_create(new_pwd_cont);
    lv_textarea_set_password_mode(dialog->new_pwd_ta, true);
    lv_obj_set_size(dialog->new_pwd_ta, 180, 40);
    lv_obj_add_event_cb(dialog->new_pwd_ta, ta_event_cb, LV_EVENT_ALL, NULL);

    // 创建按钮容器
    lv_obj_t* btn_container = lv_obj_create(dialog->obj);
    lv_obj_set_size(btn_container, 260, 50);
    lv_obj_align(btn_container, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(btn_container, 0, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);

    // 创建确认按钮
    lv_obj_t* btn_ok = lv_btn_create(btn_container);
    lv_obj_set_size(btn_ok, 120, 40);
    lv_obj_add_event_cb(btn_ok, btn_ok_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_ok = lv_label_create(btn_ok);
    lv_label_set_text(label_ok, "OK");
    lv_obj_center(label_ok);

    // 创建取消按钮
    lv_obj_t* btn_cancel = lv_btn_create(btn_container);
    lv_obj_set_size(btn_cancel, 120, 40);
    lv_obj_add_event_cb(btn_cancel, btn_cancel_event_cb, LV_EVENT_CLICKED, dialog);
    lv_obj_t* label_cancel = lv_label_create(btn_cancel);
    lv_label_set_text(label_cancel, "Cancel");
    lv_obj_center(label_cancel);

    return dialog;
}

// 更新用户列表
static void update_user_list(lv_obj_t* dd) {
    UserInfo* users = NULL;
    int count = 0;
    if (usermgr_get_all_users(&users, &count) == 0) {
        char* options = (char*)lv_mem_alloc(count * 32);
        if (options) {
            options[0] = '\0';
            int i = 0;
            for (i = 0; i < count; i++) {
                strcat(options, users[i].username);
                if (i < count - 1) {
                    strcat(options, "\n");
                }
            }
            lv_dropdown_set_options(dd, options);
            lv_mem_free(options);
        }
        usermgr_free_user_list(users);
    }
}

// 输入框事件回调
static void ta_event_cb(lv_event_t* e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* ta = lv_event_get_target(e);
    
    if (code == LV_EVENT_FOCUSED) {
        if (g_virt_kb) {
            virt_kb_show(g_virt_kb, ta);
        }
    } else if (code == LV_EVENT_DEFOCUSED) {
        if (g_virt_kb) {
            virt_kb_hide(g_virt_kb);
        }
    }
}

// 确认按钮事件回调
static void btn_ok_event_cb(lv_event_t* e) {
    user_pwd_dialog_t* dialog = (user_pwd_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    char username[32];
    lv_dropdown_get_selected_str(dialog->user_dd, username, sizeof(username));
    const char* old_pwd = lv_textarea_get_text(dialog->old_pwd_ta);
    const char* new_pwd = lv_textarea_get_text(dialog->new_pwd_ta);

    // 验证旧密码
    if (!usermgr_verify_password(username, old_pwd)) {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Old password is incorrect", NULL, true);
        lv_obj_center(mbox);
        return;
    }

    // 修改密码
    if (usermgr_change_password(username, old_pwd, new_pwd)) {
        // 关闭对话框
        if (dialog->close_cb) {
            dialog->close_cb();
        }
        lv_obj_del(dialog->obj);
        lv_mem_free(dialog);
    } else {
        lv_obj_t* mbox = lv_msgbox_create(NULL, "Error", "Failed to change password", NULL, true);
        lv_obj_center(mbox);
    }
}

// 取消按钮事件回调
static void btn_cancel_event_cb(lv_event_t* e) {
    user_pwd_dialog_t* dialog = (user_pwd_dialog_t*)lv_event_get_user_data(e);
    if (!dialog) return;

    // 关闭对话框
    if (dialog->close_cb) {
        dialog->close_cb();
    }
    lv_obj_del(dialog->obj);
    lv_mem_free(dialog);
}